package com.jcsz.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.jcsz.gulimall.product.entity.ProductAttrValueEntity;
import com.jcsz.gulimall.product.vo.AttrRespVo;
import com.jcsz.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jcsz.gulimall.product.service.AttrService;
import com.jcsz.common.utils.PageUtils;
import com.jcsz.common.utils.R;


/**
 * 商品属性
 *
 * @author 一把Po吉他
 * @email volibear@gulimall.com
 * @date 2022-10-16 08:40:06
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 修改规格参数/product/attr/update/{spuId}
     */
    @RequestMapping("/update/{spuId}")
    public R updateBaseAttrBySpuId(@PathVariable Long spuId, @RequestBody List<ProductAttrValueEntity> productAttrValueEntityList) {

        attrService.updateBaseAttrBySpuId(spuId,productAttrValueEntityList);
        return R.ok();
    }


    /**
     * 规格参数/product/attr/
     */
    @RequestMapping("/base/listforspu/{spuId}")
    public R getBaseAttrBySpuId(@PathVariable() Long spuId) {

        List<ProductAttrValueEntity> baseAttrs = attrService.getBaseAttrBySpuId(spuId);
        return R.ok().put("data", baseAttrs);
    }


    /**
     * 列表
     */
    @RequestMapping("/{attr_type}/list/{catelog_id}")
    //@RequiresPermissions("product:attr:list")
    // http://localhost:88/api/product/attr/base/list/0?t=1666483786022&page=1&limit=10&key=
    public R listPlus(@RequestParam Map<String, Object> params, @PathVariable("attr_type") String attrType, @PathVariable("catelog_id") Long catelogId) {

        PageUtils page = attrService.queryPageWrapper(params, attrType, catelogId);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
        AttrRespVo attr = attrService.getAttrById(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr) {

        attrService.saveAttr(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr) {
        attrService.updateAttrById(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
